package com.ocom.payconfig.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfShoppingCart;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GetDishesFormCartRequest;
import com.ocom.common.request.payconfig.RemoveDishesToCartRequest;
import com.ocom.common.request.payconfig.RollbackDishesCartRequest;
import com.ocom.common.request.payconfig.SubmitDishesCartRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.GetDishesFormCartVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfShoppingCart;
import com.ocom.payconfig.service.XfShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class XfShoppingCartServiceImpl extends ServiceImpl<IDaoXfShoppingCart, EntityXfShoppingCart> implements XfShoppingCartService {

    @DataSourceAspect(companyId = "companyId",type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Result addDishesToCart(EntityXfShoppingCart xfShoppingCart, Long companyId) {
        EntityXfShoppingCart entityXfShoppingCartA = super.getOne(Wrappers
                .lambdaQuery(EntityXfShoppingCart.class)
                .eq(EntityXfShoppingCart::getComId,xfShoppingCart.getComId())
                .eq(EntityXfShoppingCart::getPId,xfShoppingCart.getPId())
                .eq(EntityXfShoppingCart::getMealDate,xfShoppingCart.getMealDate())
                .eq(EntityXfShoppingCart::getMealNo,xfShoppingCart.getMealNo())
                .eq(EntityXfShoppingCart::getDishesId,xfShoppingCart.getDishesId())
                .eq(EntityXfShoppingCart::getPriceMode,xfShoppingCart.getPriceMode())
                .last("limit 1")
        );
        if(ObjectUtil.isEmpty(entityXfShoppingCartA)){
            xfShoppingCart.setState(0);
            super.save(xfShoppingCart);
            return ResultUtil.success();
        }else{
            if(!entityXfShoppingCartA.getState().equals(0)){
                return ResultUtil.error(ResultEnum.Err_4085);
            }
            entityXfShoppingCartA.setWeightNumber(entityXfShoppingCartA.getWeightNumber() + xfShoppingCart.getWeightNumber());
            super.updateById(entityXfShoppingCartA);
            return ResultUtil.success();
        }
    }

    @DataSourceAspect(companyId = "companyId",type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean removeDishesToCart(RemoveDishesToCartRequest request,Long companyId) {
        return super.remove(
                Wrappers.lambdaQuery(EntityXfShoppingCart.class)
                        .eq(EntityXfShoppingCart::getComId,request.getComId())
                        .eq(EntityXfShoppingCart::getPId,request.getPId())
                        .eq(ObjectUtil.isNotEmpty(request.getPlatOrderNo()),EntityXfShoppingCart::getPlatOrderNo,request.getPlatOrderNo())
                        .eq(ObjectUtil.isNotEmpty(request.getPointId()) && request.getPointId() > 0,EntityXfShoppingCart::getPointId,request.getPointId())
                        .eq(ObjectUtil.isNotEmpty(request.getMealDate()),EntityXfShoppingCart::getMealDate,request.getMealDate())
                        .eq(ObjectUtil.isNotEmpty(request.getMealNo()) && request.getMealNo() > 0,EntityXfShoppingCart::getMealNo,request.getMealNo())
        );
    }

    @DataSourceAspect(companyId = "companyId",type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<GetDishesFormCartVo> getDishesFormCart(GetDishesFormCartRequest request, Long companyId) {
        QueryWrapper<GetDishesFormCartVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c.com_id",request.getComId())
                .eq("c.p_id",request.getPId())
                .eq("c.state",0)
                .ge("c.meal_date", DateUtil.beginOfDay(new Date()))
                .orderByDesc("merch_id","point_id","meal_date","meal_no");
        return this.baseMapper.getDishesFormCart(queryWrapper);
    }

    @DataSourceAspect(companyId = "companyId",type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean submitDishesCart(SubmitDishesCartRequest request, Long companyId) {
        EntityXfShoppingCart entityXfShoppingCart = new EntityXfShoppingCart();
        entityXfShoppingCart.setState(1);
        entityXfShoppingCart.setPlatOrderNo(request.getPlatOrderNo());
        List<Integer> ids = request.getShoppingCart().stream().map(SubmitDishesCartRequest.ShoppingCart::getId).collect(Collectors.toList());
        if(ObjectUtil.isEmpty(ids)){
            log.info("submitDishesCart更新状态码失败，参数：【{}】", JSON.toJSONString(request));
            return false;
        }

        QueryWrapper<EntityXfShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id",request.getComId())
                .eq("p_id",request.getPId())
                .in("id",ids);
        return super.update(entityXfShoppingCart,queryWrapper);
    }

    @DataSourceAspect(companyId = "companyId",type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean rollbackDishesCart(RollbackDishesCartRequest request, Long companyId) {
        EntityXfShoppingCart entityXfShoppingCart = new EntityXfShoppingCart();
        entityXfShoppingCart.setState(0);
        entityXfShoppingCart.setPlatOrderNo("");

        QueryWrapper<EntityXfShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id",request.getComId())
                .eq("p_id",request.getPId())
                .eq("plat_order_no",request.getPlatOrderNo())
        ;
        return this.update(entityXfShoppingCart,queryWrapper);
    }
}
